package com.yjhb.android.sbz.v2.mvp.presenter

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.yjhb.android.sbz.http.HttpObserver
import com.yjhb.android.sbz.http.HttpTool
import com.yjhb.android.sbz.mvp.ConventionPresenter
import com.yjhb.android.sbz.v2.entity.LastRank
import com.yjhb.android.sbz.v2.mvp.view.V2LastRankView


/**
 *  创建者 zx
 *  创建时间 2021/4/27 9:10
 */
class V2LastRankPresenter<D>(v: V2LastRankView<D>) : ConventionPresenter<V2LastRankView<D>>(v) {


    /**
     * 获取上期榜单
     */
    fun getLastRank(rankType: Int, clazz: Class<D>) {
        view?.onLoading()
        model?.let {
            HttpTool.request(model.getNewRank(rankType, 2), object : HttpObserver<JSONObject>() {
                override fun onResult(data: JSONObject) {
                    val lastRank = LastRank<D>()
                    val rankAgent = data.getJSONArray("rankAgent")
                    val list = mutableListOf<D>()
                    lastRank.period = data.getString("period")
                    for (rankItem in rankAgent.toJavaList(JSONObject::class.java)) {
                        list.add(JSON.toJavaObject(rankItem, clazz))
                    }
                    lastRank.rankAgent = list
                    view?.onLastRank(lastRank)
                }

                override fun onError(code: Int, msg: String) {
                    view?.onFailure(code, msg)
                }
            })
        }
    }

}